package com.ying.dogying.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ying.dogying.dao.ImUserMessageDao;
import com.ying.dogying.entity.ImUserMessage;
import com.ying.dogying.entity.dto.ImUserMessageDto;
import com.ying.dogying.service.ImUserMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 李华明
 * @since 2020-06-10
 */
@Service
public class ImUserMessageServiceImpl extends ServiceImpl<ImUserMessageDao, ImUserMessage> implements ImUserMessageService {

    @Autowired
    private ImUserMessageDao imUserMessageDao;

    @Override
    public IPage<ImUserMessageDto> selectByParams(Page page, Long profileId) {

        IPage<ImUserMessageDto> messageList = imUserMessageDao.selectByParams(page, new QueryWrapper<ImUserMessageDto>()
                .eq("to_user_id", profileId)
                .orderByDesc("create_time"));

        // 把消息改成已读状态
        List<Long> ids = new ArrayList<>();
        for(ImUserMessageDto messageDto : messageList.getRecords()) {
            if(messageDto.getStatus() == 0) {
                ids.add(messageDto.getId());
            }
        }
        imUserMessageDao.updateToReaded(new QueryWrapper<ImUserMessageDto>());
        return messageList;
    }
}
